<template>
	<view>		
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''" v-for="(item,index) in classes" :key="index" @tap="tabSelect" :data-id="index" :data-number="item.number">
					{{item.title}}{{item.open_issues?`(${item.open_issues})`:''}}
				</view>
			</view>
		</scroll-view>
		<view v-for="(blog,index) in blogs" :key="index" class="cu-card article" @tap="showDetail(blog)">
			<view class="cu-item shadow">
				<view class="title"><view class="text-cut">{{blog.title}}</view></view>
				<view class="content">
					<text class="date">{{blog.createTime}}</text>
					<image v-if="blog.cover" :src="blog.cover"
								 mode="aspectFill"></image>
					<view class="desc">
						<view class="text-content2">{{blog.desc}}</view>
						<view>
							<view v-for="(label,index) in blog.labels" :key="index" class="cu-tag light sm round" style="color: white;" :style="{ backgroundColor:'#'+label.color }">{{label.name}}</view>				
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="blogs.length === 0" style="display: flex;flex-direction: column;align-items: center">
			<image src="../../static/noContent.png"
						 mode="aspectFit" style="width: 200upx"></image>
			<text>暂无内容</text>
		</view>
		<view class="cu-load load-modal" v-if="loadModal">
			<!-- <view class="cuIcon-emojifill text-orange"></view> -->
			<image src="/static/logo.png" mode="aspectFit" style="width: 150upx;height: 150upx;"></image>
		</view>
		<view class="loading-text">{{loadingText}}</view>	

	</view>
</template>

<script>
	import { queryAllClasses,queryPosts } from '../../utils/service.js'
	import { formatBlogs } from "../../utils/format";
	export default {
		data() {
			return {
				TabCur: 0,
				scrollLeft: 0,
				classes: [],
				blogs:[],
				filter:'',
				page:1,
				size:10,
				loadModal: false,
				loadingText:'没有更多的了'
			};
		},
		async onLoad(){
			this.page = 1;
			this.loadModal = true;
			this.classes = await queryAllClasses()
			this.classes.unshift({
				number:0,
				title:'最新'
			})
			this.blogs = await queryPosts(this.page,this.size,this.filter)
			this.blogs  = formatBlogs(this.blogs)
			this.loadModal = false;
		},
		methods: {
			async tabSelect(e) {
				this.page = 1;
				this.TabCur = e.currentTarget.dataset.id;
				let number = e.currentTarget.dataset.number;
				if (number === 0){
					this.filter = ''
				}else {
					this.filter = '&milestone='+number
				}
				this.loadModal = true;
				this.blogs = []
				this.blogs = await queryPosts(this.page,this.size,this.filter)
				this.blogs  = formatBlogs(this.blogs)
				this.loadModal = false;
				console.log(this.blogs)
			},
			showDetail(data){
				uni.navigateTo({
					url: '../detail/detail?id='+data.number
				})
			},
			//上拉加载，需要自己在page.json文件中配置"onReachBottomDistance"
			async onReachBottom(){
				uni.showToast({title: '加载中'});
				this.page = this.page +1;
				this.loadingText="正在加载..."
				let res = await queryPosts(this.page,this.size)
				if(res.length == 0){
					this.loadingText="没有更多的了"
					this.page = this.page - 1;
				}else{
					let blogs = formatBlogs(res)
					blogs.forEach(b => {
						this.blogs.push(b)
					})				
				}		
				console.log(this.blogs)
				
			}
		}
	}
</script>

<style lang="scss">
.loading-text{
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 60upx;
			color: #979797;
			font-size: 24upx;
		}
</style>
